package com.gome.ai.aiparter.controller;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class DictController {

    Map<String,String> dict = new HashMap<>();

    public DictController(){
        new Thread(this::init).start();
    }

    void init(){
        try (InputStream in = DictController.class.getResourceAsStream("/ecdict_map.json")){
            dict = JSON.parseObject(in,Map.class);
            log.info("词典初始化成功：size = {}",dict.size());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/dict/{word}")
    public String translation(@PathVariable("word") String word){
        return dict.get(word);
    }
}
